<?php

namespace C\L;

use Phalcon\Paginator\Factory;

class Service extends Di
{
    public $model = null;

    protected function init()
    {
        $this->setModel();
    }

  //设置服务对应的
    protected function setModel()
    {
        $this->model = null;
    }

    public function save($data)
    {
        if (!$id = $this->model->add($data)) {
            return false;
        }
        return $id;
    }

    public function saves($data)
    {
        $time = time();
        foreach ($data as &$item) {
            $item['uptime'] = $item['addtime'] = $time;
        }
        return $this->model->multiAdd($data);
    }

    public function update($id, $update, $columns = null)
    {
        return $this->model->editByPrimary($id, $update, $columns);
    }

    public function updates($update, $data, $dataType = [])
    {
        return $this->model->updates($update, $data, $dataType);
    }

    public function search($data = [], $dataType = [], $columns = [], $order = '')
    {
        if (is_array($data)) {
            return $this->model->get($data, $dataType, $columns, $order);
        }

        if (!empty($dataType)) {
            return $this->model->get(
                [$dataType => $data],
                [],
                $columns,
                $order
            );
        }

        return $this->model->getByPrimary($data, $dataType, $columns, $order);
    }

    public function searcha($data = [], $dataType = [], $columns = [], $order = '')
    {
        if (is_array($data)) {
            return $this->model->get($data, $dataType, $columns, $order, true);
        }

        if (!empty($dataType)) {
            return $this->model->get(
                [$dataType => $data],
                [],
                $columns,
                $order,
                true
            );
        }

        return $this->model->getByPrimary($data, $dataType, $columns, $order, true);
    }

    public function searchPage($data = [], $dataType = [], $columns = [], $order = '', $pageCurren = 1, $pageNum = 10)
    {
        if (!is_array($data)) {
            $data = [
            $dataType => $data
            ];

            $dataType = [];
        }

        return $this->model->search(
            $data,
            $dataType,
            $columns,
            $order,
            $pageCurren,
            $pageNum
        );
    }


    public function getCount($data = [], $dataType = [])
    {
        return $this->model->getCount($data, $dataType);
    }

    public function getSum($key, $data = [], $dataType = [])
    {
        $num = $this->model->getSum($key, $data, $dataType);
        if ($num == null) {
            return 0;
        }
        return $num;
    }

    public function getMin($key, $data = [], $dataType = [])
    {
        return $this->model->getMin($key, $data, $dataType);
    }

    public function getMax($key, $data = [], $dataType = [])
    {
        return $this->model->getMax($key, $data, $dataType);
    }

    public function getValue($key, $data = [], $dataType = [])
    {
        $data = $this->search($data, $dataType);
        if (!empty($data) && array_key_exists($key, $data)) {
            return $data[$key];
        }
        return '';
    }

    public function searchAll($data = [], $dataType = [], $columns = [], $order = '', $limit = '')
    {
        return $this->model->getAll($data, $dataType, $columns, $order, $limit);
    }

    public function getStatusConfig()
    {
        return [];
    }

    public function numIncr($key, $id, $incr = 1)
    {
        return $this->model->incrByColumn($key, $id, $incr);
    }

  //--------------- new
  /**
   * 分页
   *
   * @param Phalcon\Mvc\Model\Query\Builder $builder
   * @param int $currentPage
   * @param int $perPage
   * @return array
   **/
    protected function paginateByBuilder($builder, $currentPage, $perPage)
    {
        $options = [
        'builder' => $builder,
        'limit'   => $perPage,
        'page'    => $currentPage,
        'adapter' => 'queryBuilder',
        ];

        $pagination = Factory::load($options)->getPaginate();

        $result = [
        'items' => $pagination->items,
        'pagination' => [
        'total'        => $pagination->total_items,
        'per_page'     => $pagination->limit,
        'total_page'   => $pagination->total_pages,
        'current_page' => $pagination->current,
        ],
        ];

        return $result;
    }
}
